package com.example.controller;

import com.example.dao.UserDao;
import com.example.entity.CarEntity;
import com.example.entity.ExcelEntity;
import com.example.entity.User;
import com.example.interceptor.CarsPage;
import com.example.interceptor.Page;
import com.example.util.poi.ExcelUtil;
import com.mybatis.criteria.MyCriteria;
import com.mybatis.criteria.MyEntityCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Api("ExcelController相关接口")
public class ExcelController {
    //本来打算练习excel的，正好拿来试试cruddao
    @Autowired
    private UserDao userDao;

    @RequestMapping(value = "/insertUser",method = RequestMethod.POST)
    @ApiOperation(value = "插入user", notes = "插入user")
    public int insertUser( @RequestParam(value = "name") @ApiParam("姓名")String name,
                           @RequestParam(value = "age") @ApiParam("年龄") int age)
    {
        User user =new  User();
        user.setName(name);
        //user.setAge(age);
        return userDao.insert(user);
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除user", notes = "删除user")
    public int deleteById(@RequestParam(value = "id") @ApiParam("id") Long id)
    {
        //删除不存在的id也不会报错，值返回0，可以先查询下是否存在，抛出异常
        return userDao.deleteById(id);
    }
    @RequestMapping(value = "/updateByEntity",method = RequestMethod.PUT)
    @ApiOperation(value = "更新user，不传不变", notes = "更新user，不传不变")
    public int updateByEntity(@RequestBody  User user)
    {
        return userDao.updateByEntity(user);
    }
    @RequestMapping(value = "/updateByWholeEntity",method = RequestMethod.PUT)
    @ApiOperation(value = "更新user，不传就setNull", notes = "更新user，不传就setNull")
    public int updateByWholeEntity(@RequestBody  User user)
    {
        return userDao.updateByWholeEntity(user);
    }
    @RequestMapping(value = "/getUserById",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询User", notes = "根据id查询User")
    public User getUserById(@RequestParam  Long id)
    {
        return userDao.getEntityById(id);
    }

    @RequestMapping(value = "/getUserByNameOrAge",method = RequestMethod.GET)
    @ApiOperation(value = "查询插件 and和or", notes = "根据id查询User")
    public List<User> getUserByNameOrAge(@RequestParam  String name,@RequestParam(required = false) Integer age)
    {
        MyCriteria myCriteria = MyEntityCriteria.build();
        myCriteria = myCriteria.eq("name",name);//感觉不需要赋值给原来对象也行啊，一会试试
        if (null!=age)
        {
            myCriteria = myCriteria.or().eq("age",age);
        }
        return userDao.getEntityList(myCriteria);

    }


    @RequestMapping(value = "/getUserCount",method = RequestMethod.GET)
    @ApiOperation(value = "查询user表数量", notes = "查询user表数量")
    public Long getUserCount()
    {
        return userDao.getEntityCount();
    }
    @RequestMapping(value = "/getValueById",method = RequestMethod.GET)
    @ApiOperation(value = "根据id和表列名查询", notes = "根据id和表列名查询")
    public String getValueById(@RequestParam Long id,@RequestParam String fieldName)
    {
        return userDao.getValueById(id,fieldName);
    }
    @RequestMapping(value = "/testString",method = RequestMethod.GET)
    @ApiOperation(value = "testString", notes = "testString")
    public String testString()
    {
        return "123456789";
    }
    @RequestMapping(value = "/insertUserBatch",method = RequestMethod.POST)
    @ApiOperation(value = "批量插入user", notes = "批量插入user")
    public int insertUserBatch(@RequestBody List<User> userList)
    {
//        System.out.println(userList);完美，传不传id都行
        return userDao.insertBatch(userList);
    }
    @RequestMapping(value = "/deleteUserBatch",method = RequestMethod.DELETE)
    @ApiOperation(value = "批量插入user", notes = "批量插入user")
    public int deleteUserBatch(@RequestBody List<Long> ids)
    {
//        System.out.println(userList);完美，传不传id都行
        return userDao.deleteBatch(ids);
    }

    @RequestMapping(value = "/queryUserByPage",method = RequestMethod.GET)
    @ApiOperation(value = "批量查询user", notes = "批量查询user")
    public org.springframework.data.domain.Page<User> queryUserByPage(@RequestParam int page, @RequestParam int size,
                                                                @RequestParam(required = false) String fieldName)
    {
        Sort sort = new Sort(new  Sort.Order(Sort.Direction.ASC,fieldName));
        Pageable pageable = new PageRequest(page-1,size,sort);
        org.springframework.data.domain.Page<User> pageEntity =
                userDao.getEntityListForPage(pageable);
        return pageEntity;
    }

    @RequestMapping(value = "/getExcel2007",method = RequestMethod.GET)
    @ApiOperation(value = "生成2007的表格", notes = "生成2007的表格",produces="application/octet-stream")
    public void getExcel2007(HttpServletResponse response)
    {
        ExcelEntity excelEntity = new ExcelEntity();
        excelEntity.setId(1L);
        excelEntity.setName("朱长华");
        excelEntity.setCreateTime(new Date());
        excelEntity.setUpdateTime(new Date());

        ExcelEntity excelEntity1 = new ExcelEntity();
        excelEntity1.setId(2L);
        excelEntity1.setName("无敌");
        excelEntity1.setAge(22);
        excelEntity1.setCreateTime(new Date());
        excelEntity1.setUpdateTime(new Date());

        List<ExcelEntity> excelEntities = new ArrayList<>();
        excelEntities.add(excelEntity);
        excelEntities.add(excelEntity1);
        ExcelUtil.downExcel2007(ExcelEntity.class,excelEntities).out(response);
    }

    @RequestMapping(value = "/getExcel2003",method = RequestMethod.GET)
    @ApiOperation(value = "getExcel2003", notes = "生成2003的表格",produces="application/octet-stream")
    public void getExcel2003(HttpServletResponse response)
    {
        ExcelEntity excelEntity = new ExcelEntity();
        excelEntity.setId(1L);
        excelEntity.setName("朱长华");
        excelEntity.setCreateTime(new Date());
        excelEntity.setUpdateTime(new Date());

        ExcelEntity excelEntity1 = new ExcelEntity();
        excelEntity1.setId(2L);
        excelEntity1.setName("无敌");
        excelEntity1.setAge(22);
        excelEntity1.setCreateTime(new Date());
        excelEntity1.setUpdateTime(new Date());

        List<ExcelEntity> excelEntities = new ArrayList<>();
        excelEntities.add(excelEntity);
        excelEntities.add(excelEntity1);
        ExcelUtil.downExcel2003(ExcelEntity.class,excelEntities).out(response);
    }
}
